package gamesys.games.particlesystem.core
{
	import gamesys.games.particlesystem.effects.IEffect;

	import flash.utils.Dictionary;

	/**
	 * @author robertthurston
	 */
	public class ParticleSystemGroup
	{
		private var _particleSystems : Dictionary;
		private var _particleSystemsLength : uint;
		private var _effectIDS : Dictionary;

		public function ParticleSystemGroup()
		{
			_particleSystems = new Dictionary();
			_effectIDS = new Dictionary();
		}

		public function addParticleSystem(particleSystemID : String, particleSystem : ParticleSystem) : void
		{
			_particleSystems[particleSystemID] = particleSystem;
			_particleSystemsLength++;
		}

		public function removeParticleSystem(ID : String) : void
		{
			var particleSystem : ParticleSystem = _particleSystems[ID];

			particleSystem.stopEmitting();
			particleSystem.reset();
			particleSystem.removeAllEffects();
			particleSystem = null;

			delete _particleSystems[ID];
			
			_particleSystemsLength--;
		}

		public function addEffect(particleSystemID : String, effect : IEffect) : void
		{
			var particleSystem : ParticleSystem = _particleSystems[particleSystemID];

			particleSystem.addEffect(effect);
		}

		public function removeEffect(particleSystemID : String) : void
		{
			var particleSystem : ParticleSystem = _particleSystems[particleSystemID];
		}

		public function update() : void
		{
			for each (var particleSystem : ParticleSystem in _particleSystems)
			{
				particleSystem.update();
			}
		}

		public function addParticles(particleSystemID : String, quantity : int) : void
		{
			var particleSystem : ParticleSystem = _particleSystems[particleSystemID];

			particleSystem.addParticles(quantity);
		}

		public function addGroupParticles(quantity : int) : void
		{
			quantity = quantity / _particleSystemsLength;
			
			for each (var particleSystem : ParticleSystem in _particleSystems)
			{
				particleSystem.addParticles(quantity);
			}
		}

		public function get totalParticles() : uint
		{
			var total : uint;

			for each (var particleSystem : ParticleSystem in _particleSystems)
			{
				total += particleSystem.totalParticles;
			}

			return total;
		}
	}
}
